<?php

/**
 * 文件注释
 *
 * Author: Liam
 * Datetime: 03/29/18 00:00 PM
 */

namespace ApiBundle\Controller;

use BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

class PublicController extends BaseController
{
    /**
     * 上传图片
     *
     * @return JsonResponse
     */
    public function uploadImageAction(Request $request)
    {
        if (isset($_FILES['image']) && $_FILES['image']['name']) {
            $date = date('Ym');
            $path = $this->getParameter('dir_upload') . DIRECTORY_SEPARATOR . $date . DIRECTORY_SEPARATOR;
            $data = $this->uploadImage('image', $path, uniqid());

            if (isset($data['path'])) {
                $result = array('errorCode' => 0, 'message' => '', 'data' => array('path' => $data['path']));
            } else {
                $result = array('errorCode' => 1, 'message' => current($data['errors']));
            }
        } else {
            $result = array('errorCode' => 2, 'message' => '请上传图片');
        }

        return new JsonResponse($result);
    }
}